<?php

namespace addons\ftp;

use app\common\library\Menu;
use app\common\model\Attachment;
use think\Addons;

/**
 * 插件
 */
class Ftp extends Addons
{

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        return true;
    }

    /**
     *
     * @return mixed
     */
    public function uploadConfigInit(&$upload)
    {
        $uploadcfg = $this->getConfig('ftp');
        $uploadcfg = $uploadcfg ? $uploadcfg : [];
        $uploadcfg['ftp_usernmae'] = isset($uploadcfg['ftp_usernmae']) ? $uploadcfg['ftp_usernmae'] : '';
        $uploadcfg['ftp_password'] = isset($uploadcfg['ftp_password']) ? $uploadcfg['ftp_password'] : '';
        $uploadcfg['ftp_host'] = isset($uploadcfg['ftp_host']) ? $uploadcfg['ftp_host'] : '';
        $uploadcfg['ftp_port'] = isset($uploadcfg['ftp_port']) ? $uploadcfg['ftp_port'] : '';
        $uploadcfg['ftp_ssh'] = isset($uploadcfg['ftp_ssh']) ? $uploadcfg['ftp_ssh'] : '';
        $uploadcfg['cdn_url'] = isset($uploadcfg['cdn_url']) ? $uploadcfg['cdn_url'] : '';

        $upload['ftp_usernmae'] = isset($uploadcfg['ftp_usernmae']) ? $uploadcfg['ftp_usernmae'] : '';
        $upload['ftp_password'] = isset($uploadcfg['ftp_password']) ? $uploadcfg['ftp_password'] : '';
        $upload['ftp_host'] = isset($uploadcfg['ftp_host']) ? $uploadcfg['ftp_host'] : '';
        $upload['ftp_port'] = isset($uploadcfg['ftp_port']) ? $uploadcfg['ftp_port'] : '';
        $upload['ftp_ssh'] = isset($uploadcfg['ftp_ssh']) ? $uploadcfg['ftp_ssh'] : '';
        $upload['cdnurl'] = isset($uploadcfg['cdn_url']) ? $uploadcfg['cdn_url'] : '';
    }

    /**
     * 上传成功后
     */
    public function uploadAfter($attachment)
    {
        $config = $this->getConfig();

        if ($config['ftp_usernmae'] && $config['ftp_password']) {
            // 上传到 FTP 服务器
            if (strpos($attachment->url, '/uploads') === false) {
                $path = 'public/uploads';
            } else {
                $path = 'public';
            }
            try {
                $file = ROOT_PATH . $path . $attachment->url;
                $ofile = '';
                if (isset($attachment->original)) {
                    $ofile = ROOT_PATH . $path . $attachment->original;
                }
                $suffix = strtolower(pathinfo($attachment->url, PATHINFO_EXTENSION));
                $ossFile = str_replace('.' . $suffix, '', $file);
                $ossFilename = str_replace('.' . $suffix, '', $attachment->url);
                $FTP = new \addons\ftp\library\Auth($config['ftp_host'], $config['ftp_port'], $config['ftp_usernmae'], $config['ftp_password'], $config['ftp_ssh'], $config['ftp_pasv']);
                if (is_file($file)) {
                    $FTP->up_file($file, $config['ftp_path'] . $attachment->url);
                }
                if (is_file($ossFile . '_m.' . $suffix)) {
                    $FTP->up_file($ossFile . '_m.' . $suffix, $config['ftp_path'] . $ossFilename . '_m.' . $suffix);
                }
                if (is_file($ossFile . '_l.' . $suffix)) {
                    $FTP->up_file($ossFile . '_l.' . $suffix, $config['ftp_path'] . $ossFilename . '_l.' . $suffix);
                }
                if (is_file($ofile)) {
                    $FTP->up_file($ofile, $config['ftp_path'] . $attachment->original);
                }
                $FTP->close();
                if (isset($attachment->sha1)) {
                    $attachment->where('sha1', $attachment->sha1)->update(['storage' => 'ftp']);
                }
                // 是否删除源文件
                if ($config['delete_source']) {
                    if (is_file($file)) {
                        @unlink($file);
                    }
                    if (is_file($ossFile . '_m.' . $suffix)) {
                        @unlink($ossFile . '_m.' . $suffix);
                    }
                    if (is_file($ossFile . '_l.' . $suffix)) {
                        @unlink($ossFile . '_l.' . $suffix);
                    }
                    if (is_file($ofile)) {
                        @unlink($ofile);
                    }
                }
            } catch (\Exception $e) {
                echo json_encode(['code' => 0, 'msg' => $e->getMessage()]);
                exit;
            }
        }
    }

    /**
     * 删除成功后
     */
    public function uploadDelete($attachment)
    {
        $config = $this->getConfig();
        if (strpos($attachment->url, '/uploads') === false) {
            $path = 'public/uploads';
        } else {
            $path = 'public';
        }
        try {
            $file = ROOT_PATH . $path . $attachment->url;
            $ofile = '';
            if (isset($attachment->original)) {
                $ofile = ROOT_PATH . $path . $attachment->original;
            }
            $suffix = strtolower(pathinfo($attachment->url, PATHINFO_EXTENSION));
            $ossFile = str_replace('.' . $suffix, '', $attachment->url);
            $localFile = str_replace('.' . $suffix, '', $file);
            $FTP = new \addons\ftp\library\Auth($config['ftp_host'], $config['ftp_port'], $config['ftp_usernmae'], $config['ftp_password'], $config['ftp_ssh'], $config['ftp_pasv']);

            // 删除 oss
            $FTP->del_file($config['ftp_path'] . $attachment->url);
            $FTP->del_file($config['ftp_path'] . $ossFile . '_m.' . $suffix);
            $FTP->del_file($config['ftp_path'] . $ossFile . '_l.' . $suffix);
            if (isset($attachment->original)) {
                $FTP->del_file($config['ftp_path'] . $attachment->original);
            }
            $FTP->close();

            // 删除本地
            if (is_file($file) && $file) {
                @unlink($file);
            }
            if ($localFile) {
                if (@is_file($localFile . '_m.' . $suffix)) {
                    @unlink($localFile . '_m.' . $suffix);
                }
                if (@is_file($localFile . '_l.' . $suffix)) {
                    @unlink($localFile . '_l.' . $suffix);
                }
            }

            if (is_file($ofile) && $ofile) {
                @unlink($ofile);
            }
        } catch (\Exception $e) {
            echo json_encode(['code' => 0, 'msg' => $e->getMessage()]);
            exit;
        }
    }
}
